﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;
using System.Reflection;

namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OQualityStrategyView : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OQualityStrategyListEasyUI";
            }

        }
        protected int QSID { get { return GetRequestValue<int>("QSID", 0); } }

        private OQualityStrategyInfo _OQualityStrategyObj = null;
        protected OQualityStrategyInfo OQualityStrategyObj
        {
            get
            {
                if (_OQualityStrategyObj == null)
                    _OQualityStrategyObj = OQualityStrategyController.GetByID(QSID);
                return _OQualityStrategyObj == null ? new OQualityStrategyInfo() { } : _OQualityStrategyObj;
            }
        }

        private List<OQualityStrategyDetailInfo> _listDetail = null;
        /// <summary>
        /// 所有管制明细
        /// </summary>
        protected List<OQualityStrategyDetailInfo> ListDetail
        {
            get
            {
                if (_listDetail == null)
                    _listDetail = OQualityStrategyController.GetListDetailByQSID(QSID);
                return _listDetail == null ? new List<OQualityStrategyDetailInfo>() { } : _listDetail;
            }
        }

        private List<OQualityStrategyAttachmentInfo> _listAttDetail = null;
        /// <summary>
        /// 所有附件明细
        /// </summary>
        protected List<OQualityStrategyAttachmentInfo> ListAttDetail
        {
            get
            {
                if (_listAttDetail == null)
                    _listAttDetail = OQualityStrategyController.GetListAttachmentByQSID(QSID);
                return _listAttDetail == null ? new List<OQualityStrategyAttachmentInfo>() { } : _listAttDetail;
            }
        }

        protected void binddate()
        {
            string AttType = "duicireport";
            // rpSendsvrBulletin
            AttType = "duicireport";
            List<OQualityStrategyAttachmentInfo> _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            if (_attschemelist.Count <= 0)
            {
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
            }
            this.rpDuiceReportAtt.DataSource = _attschemelist;
            this.rpDuiceReportAtt.DataBind();

            AttType = "faultdescription";
            _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            if (_attschemelist.Count <= 0)
            {
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
            }
            this.rpfaultdescription.DataSource = _attschemelist;
            this.rpfaultdescription.DataBind();

            AttType = "causeanalysis";
            _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            if (_attschemelist.Count <= 0)
            {
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });
            }
            this.rpcauseanalysis.DataSource = _attschemelist;
            this.rpcauseanalysis.DataBind();

            ///////临时对策
            AttType = "ldc_scheme";
            _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();           
            this.rpLDCSchemeAtt.DataSource = _attschemelist;
            this.rpLDCSchemeAtt.DataBind();
            // rpSendsvrBulletin
            AttType = "ldc_sendsvrbulletin";
            _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();           
            this.rpLDCSendsvrBulletin.DataSource = _attschemelist;
            this.rpLDCSendsvrBulletin.DataBind();

            //
            string QSClass = "p";
            List<OQualityStrategyDetailInfo> listdetail = ListDetail.Where(e => e.QSType == "l" && e.QSClass == QSClass).ToList();
            this.rpLDCPartList.DataSource = listdetail;
            this.rpLDCPartList.DataBind();
            //统计
            DataTable dt = new DataTable();
            dt.Columns.Add("存储位置"); dt.Columns.Add("数量"); dt.Columns.Add("处理完成数量"); dt.Columns.Add("最后处理时间");
            string[] pStorageLocations = new string[] { "供应商", "厂内原料仓库", "厂内配件仓库", "其他位置" };
            string[] mStorageLocations = new string[] { "厂内下线机器", "厂内库存机器", "在途机器", "经销商库存机", "用户机器" };
            foreach(string itmstr in pStorageLocations)
            {
                List<OQualityStrategyDetailInfo> listtemp=listdetail.Where(e => e.StorageLocation == itmstr).ToList();
                string 数量 = listtemp.Count().ToString();
                string 处理完成数量 = listtemp.Where(e => e.IsProgress == "完成").ToList().Count().ToString();
                string 最后处理时间 = "";
                try
                {
                    最后处理时间 = listtemp.Where(e => e.IsProgress == "完成").ToList().OrderByDescending(e => e.ProgressDate).FirstOrDefault().ProgressDate;
                }
                catch{
                    最后处理时间="";
                }
                
                DataRow dr=dt.NewRow();
                dr["存储位置"] = itmstr;
                dr["数量"] = 数量;
                dr["处理完成数量"] = 处理完成数量;
                dr["最后处理时间"] = 最后处理时间;
                dt.Rows.Add(dr);
            }

            this.rpLDCPartTJ.DataSource = dt;
            this.rpLDCPartTJ.DataBind();

            ///rpSerList
            ///
            QSClass = "m";
            listdetail = ListDetail.Where(e => e.QSType == "l" && e.QSClass == QSClass).ToList();
            this.rpLDCSerList.DataSource = listdetail;
            this.rpLDCSerList.DataBind();
            //统计
            dt.Rows.Clear();
            //dt = new DataTable();
           // dt.Columns.Add("存储位置"); dt.Columns.Add("数量"); dt.Columns.Add("处理完成数量"); dt.Columns.Add("最后处理时间");
           // string[] pStorageLocations = new string[] { "供应商", "厂内原料仓库", "厂内配件仓库", "其他位置" };
           // string[] mStorageLocations = new string[] { "厂内下线机器", "厂内库存机器", "在途机器", "经销商库存机", "用户机器" };
            foreach (string itmstr in mStorageLocations)
            {
                List<OQualityStrategyDetailInfo> listtemp = listdetail.Where(e => e.StorageLocation == itmstr).ToList();
                string 数量 = listtemp.Count().ToString();
                string 处理完成数量 = listtemp.Where(e => e.IsProgress == "完成").ToList().Count().ToString();
                string 最后处理时间 = "";
                try
                {
                    最后处理时间 = listtemp.Where(e => e.IsProgress == "完成").ToList().OrderByDescending(e => e.ProgressDate).FirstOrDefault().ProgressDate;
                }
                catch
                {
                    最后处理时间 = "";
                }

                DataRow dr = dt.NewRow();
                dr["存储位置"] = itmstr;
                dr["数量"] = 数量;
                dr["处理完成数量"] = 处理完成数量;
                dr["最后处理时间"] = 最后处理时间;
                dt.Rows.Add(dr);
            }

            this.rpLDCSerTJ.DataSource = dt;
            this.rpLDCSerTJ.DataBind();
            /////////永久对策

            AttType = "ydc_scheme";
            _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            this.rpYDCSchemeAtt.DataSource = _attschemelist;
            this.rpYDCSchemeAtt.DataBind();
            // rpSendsvrBulletin
            AttType = "ydc_sendsvrbulletin";
            _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            this.rpYDCSendsvrBulletin.DataSource = _attschemelist;
            this.rpYDCSendsvrBulletin.DataBind();

            //
            QSClass = "p";
            listdetail = ListDetail.Where(e => e.QSType == "y" && e.QSClass == QSClass).ToList();
            this.rpYDCPartList.DataSource = listdetail;
            this.rpYDCPartList.DataBind();
            //统计
            dt.Rows.Clear();       
            foreach (string itmstr in pStorageLocations)
            {
                List<OQualityStrategyDetailInfo> listtemp = listdetail.Where(e => e.StorageLocation == itmstr).ToList();
                string 数量 = listtemp.Count().ToString();
                string 处理完成数量 = listtemp.Where(e => e.IsProgress == "完成").ToList().Count().ToString();
                string 最后处理时间 = "";
                try
                {
                    最后处理时间 = listtemp.Where(e => e.IsProgress == "完成").ToList().OrderByDescending(e => e.ProgressDate).FirstOrDefault().ProgressDate;
                }
                catch
                {
                    最后处理时间 = "";
                }

                DataRow dr = dt.NewRow();
                dr["存储位置"] = itmstr;
                dr["数量"] = 数量;
                dr["处理完成数量"] = 处理完成数量;
                dr["最后处理时间"] = 最后处理时间;
                dt.Rows.Add(dr);
            }

            this.rpYDCPartTJ.DataSource = dt;
            this.rpYDCPartTJ.DataBind();
            ///rpSerList
            QSClass = "m";
            listdetail = ListDetail.Where(e => e.QSType == "y" && e.QSClass == QSClass).ToList();
            this.rpYDCSerList.DataSource = listdetail;
            this.rpYDCSerList.DataBind();
            //统计
            dt.Rows.Clear();          
            foreach (string itmstr in mStorageLocations)
            {
                List<OQualityStrategyDetailInfo> listtemp = listdetail.Where(e => e.StorageLocation == itmstr).ToList();
                string 数量 = listtemp.Count().ToString();
                string 处理完成数量 = listtemp.Where(e => e.IsProgress == "完成").ToList().Count().ToString();
                string 最后处理时间 = "";
                try
                {
                    最后处理时间 = listtemp.Where(e => e.IsProgress == "完成").ToList().OrderByDescending(e => e.ProgressDate).FirstOrDefault().ProgressDate;
                }
                catch
                {
                    最后处理时间 = "";
                }

                DataRow dr = dt.NewRow();
                dr["存储位置"] = itmstr;
                dr["数量"] = 数量;
                dr["处理完成数量"] = 处理完成数量;
                dr["最后处理时间"] = 最后处理时间;
                dt.Rows.Add(dr);
            }

            this.rpYDCSerTJ.DataSource = dt;
            this.rpYDCSerTJ.DataBind();

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            binddate();
        }
    }
}